<?php

use Illuminate\Database\Eloquent\Model;

class attachmentModel extends Model
{
    protected $table = 'attachment';
    protected $primaryKey = 'id';

    protected function serializeDate($date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    protected static function booted()
    {
        //限定查询自营全局作用域
        static::addGlobalScope('dianpu_id', function (Illuminate\Database\Eloquent\Builder $builder) {
            $builder->where('dianpu_id', '=', 0);
        });
    }

    public function newattachment($options)
    {
        $info = pathinfo($options['remote_url']);
        $img  = getimagesize($options['remote_url']);
        $size = round($options['file_size'] / 1024, 2) . 'K';
        $dianpu_id = $options['dianpu_id']??0;
        $row  = [
            'dianpu_id'  => $dianpu_id,
            'cat_id'     => $options['cat_id'],
            'url'        => $options['remote_url'],
            'name'       => $info['basename'],
            'type'       => $info['extension'],
            'width'      => $img[0],
            'height'     => $img[1],
            'mime'       => $img['mime'],
            'size'       => $size,
            'created_at' => date("Y-m-d H:i:s")
        ];
        $this->insert($row);
    }

}